package prolog.io;
import java.io.IOException;
import java.io.Writer;
import prolog.terms.Sink;
import prolog.terms.Term;
import prolog.terms.Int;
 import prolog.terms.Prog;




object CharWriter extends Sink
{
     public writer: Writer ;
  
     def this(s: String,p: Prog) extends Sink(p)=
        {
        this.writer=IO.toFileWriter(s);
         }
  
     def this(p: Prog) extends Sink(p)
        {
         this.writer=IO.output;
        }
  
     def putElement(Term t):int = 
       {
         if(null==writer)
         return 0;
          try 
            {
            char ch=(char)((Int)t).intValue();
             writer.write(ch);
             }
         catch 
         {
            return 0;
         }
        return 1;
       }
  
    def stop():unit =
        {
            if(null!=writer&&IO.output!=writer)
            {
                try
                    {
                     writer.close();
                     } catch {
                             }
              writer=null;
             }
         }
}

 